/**
 * Copyright (c) 2020 qar All rights reserved.
 * <p>
 * https://www.qar.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.example.controller;

import com.example.dto.GenFieldTypeDTO;
import com.example.entity.BaseClassEntity;
import com.example.model.page.PageData;
import com.example.model.api.Result;
import com.example.entity.FieldTypeEntity;
import com.example.service.FieldTypeService;
import com.example.utils.ConvertUtils;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

/**
 * 字段类型管理
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@Tag(name = "字段类型管理")
@RequestMapping("fieldtype")
public class FieldTypeController {
    @Autowired
    private FieldTypeService fieldTypeService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageData<GenFieldTypeDTO>> page(@RequestParam Map<String, Object> params){
        PageData<GenFieldTypeDTO> page = fieldTypeService.page(params);

        return new Result<PageData<GenFieldTypeDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "详情")
    public Result<GenFieldTypeDTO> get(@PathVariable("id") Long id){
        AssertUtils.isNull(id, "不能为空");
        FieldTypeEntity data = fieldTypeService.selectById(id);

        return new Result<GenFieldTypeDTO>().ok(ConvertUtils.sourceToTarget(data, GenFieldTypeDTO.class));
    }

    @GetMapping("list")
    @Operation(summary = "列表")
    public Result<Set<String>> list(){
        Set<String> set = fieldTypeService.list();

        return new Result<Set<String>>().ok(set);
    }

    @PostMapping
    @Operation(summary = "新增")
    public Result<String> save(@RequestBody GenFieldTypeDTO genFieldTypeDTO){
        ValidatorUtils.validateEntity(genFieldTypeDTO);
        fieldTypeService.insert(ConvertUtils.sourceToTarget(genFieldTypeDTO, FieldTypeEntity.class));

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody GenFieldTypeDTO genFieldTypeDTO){
        ValidatorUtils.validateEntity(genFieldTypeDTO, UpdateGroup.class, DefaultGroup.class);
        fieldTypeService.updateById(ConvertUtils.sourceToTarget(genFieldTypeDTO, FieldTypeEntity.class));

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody Long[] ids){
        fieldTypeService.deleteBatchIds(Arrays.asList(ids));

        return new Result<String>().ok("ok");
    }
}